<?php
namespace app\agent\controller;

use think\Controller;
use think\Response;
use think\exception\HttpResponseException;
use think\Request;
class Common extends Controller
{
    protected $responseType = 'json';
    protected $tdb = 'fq_';
    protected $userInfo = [];
    public function initialize(){
        // if($this->request->controller() === 'Login'){
        //      $this->redirect('index/index');
        // }
        if(session('?Agent') == false){
            $this->redirect('login/index');
        }else{
            $this->userInfo = session('Agent');
            $this->userInfo  = (new \app\common\model\Agent())->where(['id' => $this->userInfo['id']])->find();
            $this->assign("userInfo",$this->userInfo);
            return true;
        }
    }
    protected function successResult($msg = '成功', $data = null, $count = 0,$code = 0, $type = null, array $header = []){
        $this->result($msg, $data,$count, $code, $type, $header,1);
	}

    protected function errorResult($msg = '失败', $data = null, $code = 1, $type = null, array $header = [])
	{
        $this->result($msg, $data,0, $code, $type, $header , 0);
	}
      /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型，支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function result($msg, $data = null,$count = 0, $code = 0, $type = null, array $header = [] , $state=0)
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'count' => $count,
            'data' => $data,
            'state' => $state,
        ];
        // 如果未设置类型则自动判断
        $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);

        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            //未设置状态码,根据code值判断
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }
        $response = Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }
    public function images()
    {
        return $this->result("成功",((new \app\common\controller\Upload())->image()),'','','',[],1);
    }
    public function get_city()
    {
        $id = intval(request()->param('id'));
        return (new \app\common\model\City())->where('ParentId', $id)->field('id,Name')->select();
    }

 
}
